#include "printer.h"

printer::printer(QWidget *parent) :
    QWidget(parent)
{
    if(!QSqlDatabase::contains("BGBBconex"))
        QSqlDatabase::addDatabase("QSQLITE","BGBBconex");
    if(!QSqlDatabase::contains("KGBBconex"))
        QSqlDatabase::addDatabase("QSQLITE","KGBBconex");
    if(!QSqlDatabase::contains("CBBconex"))
        QSqlDatabase::addDatabase("QSQLITE","CBBconex");
    if(!QSqlDatabase::contains("SubBLconex"))
        QSqlDatabase::addDatabase("QSQLITE","SubBLconex");
    if(!QSqlDatabase::contains("SVKLconex"))
        QSqlDatabase::addDatabase("QSQLITE","SVKLconex");
    if(!QSqlDatabase::contains("SVLconex"))
        QSqlDatabase::addDatabase("QSQLITE","SVLconex");


}

QString printer::multi_line(QString data, int width){
    QString buffer,line,data2=data;

    if(data.count(" ")==0){
        buffer=data;

        for(int i=0;i<data.size();i+=width+2){
            buffer.insert(i+width,"\n");
        }
    }
    else{
        while(!data2.isEmpty()){
            QString opc = shift_String(data2);
            QStringList list;
            list = opc.split(QRegExp("\\s+"));

            for(int i = 0;(i<list.size())|(i==1000);i++){
                if(list.at(i).size() < width && (line.size() + list.at(i).size()) < width){
                    line.append(list.at(i)).append(" ");
                }
                else{
                    buffer.append(line).append("\n");
                    line.clear();
                    line = list.at(i);
                    line.append(" ");
                }
            }

            if(!line.isEmpty()){
                buffer.append(line);
                line.clear();
            }
            buffer.append("\n");
        }
    }
    return buffer;
}

QString printer::shift_String(QString &msg){
    QString buffer;

    int shift = msg.indexOf("&&");

    for(int j=0;j<shift; j++){
        buffer.append(msg.at(j));
    }

    msg.remove(0,shift);
    msg.remove(0,2); // borra &&
    return buffer;
}

void printer::imprimir_resumen(QStringList &bufList){
    int X=80, Y=80; // posicion inicial
    int W=610, H=840; // dimension de la boleta de resumen
    int aux=0,spacer;

    for(int i=0;i<bufList.count();i++){
        if(i!=0 && i<=(bufList.count()-3)){
            aux+=bufList.at(i).count("\n")*15;
            if((i%2)!=0){
                aux+=15;}
        }
        else{
            aux+=bufList.at(i).count("\n")*20;
            if(((i%2)!=0)||(i==0)){
                aux+=20;}
        }
    }

    if(aux<H)
        spacer=(H-(aux+185))/16;
    else
        spacer=0;

    QPrinter printer;
    printer.setPaperSize(QPrinter::Letter);
    printer.setFullPage(true);
    QPrintDialog *dialog = new QPrintDialog(&printer, this);
    dialog->setWindowTitle(tr("Print Document"));
    if (dialog->exec( ) != QDialog::Accepted)
        return;

    QPainter painter;
    painter.begin(&printer);

// Imprime el encabezado
    painter.setFont(QFont("Courier", 16, QFont::Bold,true));
    painter.setPen(QColor("dark red"));
    painter.drawText(QRect(X,Y+2*spacer,W-20,30),Qt::AlignCenter|Qt::AlignBottom,"ASAMBLEA INSTITUCIONAL REPRESENTATIVA");
    //painter.drawRect(X,Y+2*spacer,W-20,30);
    Y+=30+2*spacer;

    painter.setFont(QFont("Courier", 12, QFont::Bold,true));
    painter.setPen(QColor(85,85,0));
    painter.drawText(QRect(X,Y,W-20,15),Qt::AlignCenter|Qt::AlignVCenter,"Sistema embebido de Votacion Electronica(SeVE)");
    //painter.drawRect(X,Y,W-20,15);
    Y+=15;

// Imprime la descripcion de la propuesta
    painter.setFont(QFont("Courier", 11, QFont::Bold,false));
    painter.setPen(QColor(0,0,0));

    painter.drawText(QRect(X,Y+spacer,W-20,15),Qt::AlignCenter|Qt::AlignVCenter,"RESULTADO DE VOTACION");
    Y+=spacer;

    aux = bufList.at(0).count("\n")+1;
    painter.drawText(QRect(X,Y+1.5*spacer,W-20,(20*aux)),Qt::AlignCenter|Qt::AlignVCenter, bufList.at(0));
    //painter.drawRect(X,Y+1.5*spacer,W-20,(20*aux));
    Y+=20*aux+1.5*spacer;

// Imprime los resultados
    painter.setFont(QFont("Courier", 10, QFont::Normal,false));
    painter.setPen(QColor(0,0,0));

    aux = bufList.at(1).count("\n")+1;
    painter.drawText(QRect(X,Y+1.5*spacer,W-120,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(1));
    painter.drawText(QRect(W+X-120,Y+1.5*spacer,100,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(2));
    //painter.drawRect(X,Y+1.5*spacer,W-120,(15*aux));
    //painter.drawRect(W+X-120,Y+1.5*spacer,100,(15*aux));
    Y+=15*aux+1.5*spacer;

    aux = bufList.at(3).count("\n")+1;
    painter.drawText(QRect(X,Y+spacer,W-120,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(3));
    painter.drawText(QRect(W+X-120,Y+spacer,100,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(4));
    //painter.drawRect(X,Y+spacer,W-120,(15*aux));
    //painter.drawRect(W+X-120,Y+spacer,100,(15*aux));
    Y+=15*aux+spacer;

    if(bufList.size()>9){
        aux = bufList.at(5).count("\n")+1;
        painter.drawText(QRect(X,Y+spacer,W-120,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(5));
        painter.drawText(QRect(W+X-120,Y+spacer,100,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(6));
//        painter.drawRect(X,Y+spacer,W-120,(15*aux));
//        painter.drawRect(W+X-120,Y+spacer,100,(15*aux));
        Y+=15*aux+spacer;
    }

    if(bufList.size()>11){
        aux = bufList.at(7).count("\n")+1;
        painter.drawText(QRect(X,Y+spacer,W-120,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(7));
        painter.drawText(QRect(W+X-120,Y+spacer,100,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(8));
//        painter.drawRect(X,Y+spacer,W-120,(15*aux));
//        painter.drawRect(W+X-120,Y+spacer,100,(15*aux));
        Y+=15*aux+spacer;
    }

    if(bufList.size()>13){
        aux = bufList.at(9).count("\n")+1;
        painter.drawText(QRect(X,Y+spacer,W-120,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(9));
        painter.drawText(QRect(W+X-120,Y+spacer,100,(15*aux)),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(10));
//        painter.drawRect(X,Y+spacer,W-120,(15*aux));
//        painter.drawRect(W+X-120,Y+spacer,100,(15*aux));
        Y+=15*aux+spacer;
    }

    painter.drawText(QRect(X,Y+spacer,W-120,15),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(bufList.size()-4));
    painter.drawText(QRect(W+X-120,Y+spacer,100,15),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(bufList.size()-3));
    Y+=15+spacer;

    painter.setFont(QFont("Courier", 12, QFont::Bold,false));
    painter.setPen(QColor(0,90,0));

    painter.drawText(QRect(X,Y+spacer,W-120,20),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(bufList.size()-2));
    painter.drawText(QRect(W+X-120,Y+spacer,100,20),Qt::AlignCenter|Qt::AlignVCenter,bufList.at(bufList.size()-1));
//    painter.drawRect(X,Y+spacer,W-120,(20*aux));
//    painter.drawRect(W+X-120,Y+spacer,100,(20*aux));
    Y+=20 + spacer;

    painter.setPen(QColor(0,0,0));
    painter.drawText(QRect(X,Y+spacer,W,20),Qt::AlignLeft|Qt::AlignVCenter,"OBSERVACIONES");
    Y+=40+spacer;
    painter.drawLine(X,Y,X+W-20,Y);
    Y+=20;
    painter.drawLine(X,Y,X+W-20,Y);
    Y+=20;
    painter.drawLine(X,Y,X+W-20,Y);
    Y+=20;
    painter.drawLine(X,Y,X+W-20,Y);
    Y+=20;
    painter.drawLine(X,Y,X+W-20,Y);
    //Y+=40;

    painter.drawLine(X+W/4-100,Y+3*spacer,X+W/4+100,Y+3*spacer);
    painter.drawLine(X+3*W/4-100,Y+3*spacer,X+3*W/4+100,Y+3*spacer);
    painter.drawText(QRect(X,Y+3*spacer,W/2-30,20),Qt::AlignCenter|Qt::AlignVCenter,"Fiscal");
    painter.drawText(QRect(X+W/2,Y+3*spacer,W/2,20),Qt::AlignCenter|Qt::AlignVCenter,"Fiscal");
    Y+=3*spacer+20;
    Y+=2*spacer;
    QPen pen;
    pen.setBrush(QBrush(Qt::Dense7Pattern));
    pen.setColor(QColor(0,0,85));
    pen.setJoinStyle(Qt::MiterJoin);
    pen.setWidth(2);
    painter.setPen(pen);
    painter.drawRect(70,70,W,H);
    pen.setWidth(5);
    painter.setPen(pen);
    painter.drawRect(65,65,W+10,H+10);
    painter.end();
}

void printer::imprimir_informes(QString desc_prop){
    desc_prop.remove(desc_prop.indexOf(":"),desc_prop.size());
    desc_prop.replace(" ","_");

    QPrinter printer;
    QPrintDialog *dialog = new QPrintDialog(&printer, this);
    dialog->setWindowTitle(tr("Print Document"));
    if (dialog->exec( ) != QDialog::Accepted)
        return;

    QPainter painter;
    painter.begin(&printer);

    BGBB(painter,printer,desc_prop);
    printer.newPage();
    KGBB(painter,printer,desc_prop);
    printer.newPage();
    CBB(painter,printer,desc_prop);
    printer.newPage();
    SubBallotList(painter,printer,desc_prop);
    printer.newPage();
    SubVotingKeyList(painter,printer,desc_prop);
    printer.newPage();
    SubVoteList(painter,printer,desc_prop);
    painter.end();
}

void printer::BGBB(QPainter &painter, QPrinter &printer, QString prop){
    int X=60,Y=50;
    int W=630,H=880;
    int page=1;

    QDir BGBBdir(QDir::currentPath());
    BGBBdir.cd(prop);
    BGBBdir.cd("BG");

// Crea la conexion
    QSqlDatabase BGBB = QSqlDatabase::database("BGBBconex",false);
    BGBB.setDatabaseName(BGBBdir.absoluteFilePath("BGBB.db"));

    if(!BGBB.open()){
        return;
    }

// Crea el modelo
    QSqlTableModel BGBBmodel(0,BGBB);
    BGBBmodel.setTable("BGBB");
    BGBBmodel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    BGBBmodel.select();


    for (int i = 0; i < BGBBmodel.rowCount(); i+=2) {

        if(Y==50){
        // Imprime el encabezado
            painter.setFont(QFont("Courier", 12, QFont::Bold,true));
            painter.setPen(QColor("dark red"));
            painter.drawText(QRect(X,Y,W,30),Qt::AlignCenter|Qt::AlignBottom,"Ballot Generator's Bulletin Board");
            Y+=50;

            painter.setPen(QColor(0,0,85));
            painter.drawText(QRect(X+W/4-20,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"B");
            painter.drawText(QRect(X+3*W/4-20,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"B");

            painter.setFont(QFont("Courier", 8, QFont::Normal,false));
            painter.drawText(QRect(X+W/4+10,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"s");
            painter.drawText(QRect(X+3*W/4+10,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"s");

            painter.setFont(QFont("Courier", 12, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-35,Y,70,15),Qt::AlignCenter|Qt::AlignVCenter,"H( ,e )");
            painter.drawText(QRect(X+3*W/4-35,Y,70,15),Qt::AlignCenter|Qt::AlignVCenter,"H( ,e )");
            Y+=30;
            painter.drawText(X+W/2,Y+H+20,QString::number(page));
        }

        if(i%4==0){
            painter.setPen(QColor(230,230,230));
            painter.setBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));}
        else{
            painter.setPen(QColor(255,255,255));
            painter.setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));}

        painter.drawRect(QRectF(X+W/4-175,Y-4,320,20));
        painter.drawRect(QRect(X+3*W/4-145,Y-4,320,20));


        painter.setFont(QFont("Courier", 10, QFont::Normal,false));
        painter.setPen(QColor(0,0,0));
        painter.drawText(QRect(X+W/4-175,Y,320,12),Qt::AlignHCenter|Qt::AlignVCenter,BGBBmodel.record(i).value("H").toString());
        painter.drawText(QRect(X+3*W/4-145,Y,320,12),Qt::AlignHCenter|Qt::AlignVCenter,BGBBmodel.record(i+1).value("H").toString());

        Y+=20;

        if (Y==(H+50)){
            printer.newPage();
            Y=50;
            page++;
        }
    }
}

void printer::KGBB(QPainter &painter, QPrinter &printer, QString prop){
    int X=60,Y=50;
    int W=630,H=880;
    int page=1;

    QDir KGBBdir(QDir::currentPath());
    KGBBdir.cd(prop);
    KGBBdir.cd("KG");

// Crea la conexion
    QSqlDatabase KGBB = QSqlDatabase::database("KGBBconex",false);
    KGBB.setDatabaseName(KGBBdir.absoluteFilePath("KGBB.db"));

    if(!KGBB.open()){
        return;
    }

// Crea el modelo
    QSqlTableModel KGBBmodel(0,KGBB);
    KGBBmodel.setTable("KGBB");
    KGBBmodel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    KGBBmodel.select();



    for (int i = 0; i < KGBBmodel.rowCount(); i++) {

        if(Y==50){
        // Imprime el encabezado
            painter.setFont(QFont("Courier", 12, QFont::Bold,true));
            painter.setPen(QColor("dark red"));
            painter.drawText(QRect(X,Y,W,30),Qt::AlignCenter|Qt::AlignBottom,"Key Generator's Bulletin Board");
            Y+=50;

            painter.setPen(QColor(0,0,85));
            painter.setFont(QFont("Courier", 8, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-10,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"v");
            painter.drawText(QRect(X+W/4+20,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");

            painter.drawText(QRect(X+3*W/4-10,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"v");
            painter.drawText(QRect(X+3*W/4+20,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");

            painter.setFont(QFont("Courier", 12, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-35,Y,80,15),Qt::AlignCenter|Qt::AlignVCenter,"H(e ,e )");
            painter.drawText(QRect(X+3*W/4-35,Y,80,15),Qt::AlignCenter|Qt::AlignVCenter,"H(e ,d )");
            Y+=30;
            painter.drawText(X+W/2,Y+H+20,QString::number(page));
        }

        if(i%2==0){
            painter.setPen(QColor(230,230,230));
            painter.setBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));}
        else{
            painter.setPen(QColor(255,255,255));
            painter.setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));}
        //painter.drawRect();
        painter.drawRect(QRectF(X+W/4-175,Y-4,W+40,20));


        painter.setFont(QFont("Courier", 10, QFont::Normal,false));
        painter.setPen(QColor(0,0,0));
        painter.drawText(QRect(X+W/4-175,Y,320,12),Qt::AlignHCenter|Qt::AlignVCenter,KGBBmodel.record(i).value("Hez").toString());
        painter.drawText(QRect(X+3*W/4-145,Y,320,12),Qt::AlignHCenter|Qt::AlignVCenter,KGBBmodel.record(i).value("Hdz").toString());

        Y+=20;

        if (Y==(H+50)){
            printer.newPage();
            Y=50;
            page++;
        }
    }
}

void printer::CBB(QPainter &painter, QPrinter &printer, QString prop){
    int X=60,Y=50;
    int W=630,H=880;
    int page=1;

    QDir CBBdir(QDir::currentPath());
    CBBdir.cd(prop);
    CBBdir.cd("C");

// Crea la conexion
    QSqlDatabase CBB = QSqlDatabase::database("CBBconex",false);
    CBB.setDatabaseName(CBBdir.absoluteFilePath("CBB.db"));

    if(!CBB.open()){
        return;
    }

// Crea el modelo
    QSqlTableModel CBBmodel(0,CBB);
    CBBmodel.setTable("CBB");
    CBBmodel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    CBBmodel.select();

    for (int i = 0; i < CBBmodel.rowCount(); i+=2) {

        if(Y==50){
        // Imprime el encabezado
            painter.setFont(QFont("Courier", 12, QFont::Bold,true));
            painter.setPen(QColor("dark red"));
            painter.drawText(QRect(X,Y,W,30),Qt::AlignCenter|Qt::AlignBottom,"Counter Generator's Bulletin Board");
            Y+=50;

            painter.setPen(QColor(0,0,85));
            painter.drawText(QRect(X+W/4-23,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"V'");
            painter.drawText(QRect(X+3*W/4-23,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"V'");

            painter.setFont(QFont("Courier", 8, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-43,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");
            painter.drawText(QRect(X+W/4+37,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"2");
            painter.drawText(QRect(X+3*W/4-43,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");
            painter.drawText(QRect(X+3*W/4+37,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"2");


            painter.setFont(QFont("Courier", 12, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-70,Y,140,15),Qt::AlignCenter|Qt::AlignVCenter,"H(E ( ,PVID ))");
            painter.drawText(QRect(X+3*W/4-70,Y,140,15),Qt::AlignCenter|Qt::AlignVCenter,"H(E ( ,PVID ))");
            Y+=30;
            painter.drawText(X+W/2,Y+H+20,QString::number(page));
        }

        if(i%4==0){
            painter.setPen(QColor(230,230,230));
            painter.setBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));}
        else{
            painter.setPen(QColor(255,255,255));
            painter.setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));}

        painter.drawRect(QRectF(X+W/4-175,Y-4,320,20));
        painter.drawRect(QRect(X+3*W/4-145,Y-4,320,20));

        painter.setFont(QFont("Courier", 10, QFont::Normal,false));
        painter.setPen(QColor(0,0,0));
        painter.drawText(QRect(X+W/4-175,Y,320,12),Qt::AlignHCenter|Qt::AlignVCenter,CBBmodel.record(i).value("H").toString());
        painter.drawText(QRect(X+3*W/4-145,Y,320,12),Qt::AlignHCenter|Qt::AlignVCenter,CBBmodel.record(i+1).value("H").toString());

        Y+=20;

        if (Y==(H+50)){
            printer.newPage();
            Y=50;
            page++;
        }
    }
}

void printer::SubBallotList(QPainter &painter, QPrinter &printer, QString prop){
    int X=60,Y=50;
    int W=630,H=880;
    int page=1,aux;

    QDir SBLdir(QDir::currentPath());
    SBLdir.cd(prop);
    SBLdir.cd("BG");

    QSqlDatabase SBL = QSqlDatabase::database("SubBLconex",false);
    SBL.setDatabaseName(SBLdir.absoluteFilePath("SubBallotList.db"));
    if(!SBL.open()){
        return;
    }

// Crea el modelo
    QSqlTableModel SBLmodel(0,SBL);
    SBLmodel.setTable("SubBallotList");
    SBLmodel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    SBLmodel.select();

    for (int i = 0; i < SBLmodel.rowCount(); i++) {

        if(multi_line(SBLmodel.record(i).value("PVID").toString(),20).count("\n") <
           multi_line(SBLmodel.record(i).value("B").toString(),60).count("\n"))
            aux=multi_line(SBLmodel.record(i).value("B").toString(),60).count("\n")+1;
        else
            aux = multi_line(SBLmodel.record(i).value("PVID").toString(),20).count("\n")+1;

        if((Y+13*aux + 8)>=(H+50)){
            printer.newPage();
            Y=50;
            page++;
        }

        if(Y==50){
        // Imprime el encabezado
            painter.setFont(QFont("Courier", 12, QFont::Bold,true));
            painter.setPen(QColor("dark red"));
            painter.drawText(QRect(X,Y,W,30),Qt::AlignCenter|Qt::AlignBottom,"Sub Ballot List");
            Y+=50;

            painter.setPen(QColor(0,0,85));
            painter.setFont(QFont("Courier", 8, QFont::Normal,false));
            painter.drawText(QRect(X+95,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"1");

            painter.setFont(QFont("Courier", 12, QFont::Normal,false));
            painter.drawText(QRect(X+60,Y,40,15),Qt::AlignCenter|Qt::AlignVCenter,"PVID");
            painter.drawText(QRect(X+W/2+100,Y,10,15),Qt::AlignCenter|Qt::AlignVCenter,"B");
            Y+=30;
            painter.drawText(X+W/2,Y+H+20,QString::number(page));
        }

        if(i%2==0){
            painter.setPen(QColor(230,230,230));
            painter.setBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));}
        else{
            painter.setPen(QColor(255,255,255));
            painter.setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));}

        painter.drawRect(QRectF(X,Y-4,160,13*aux + 8));
        painter.drawRect(QRect(X+180,Y-4,500,13*aux + 8));

        painter.setFont(QFont("Courier", 10, QFont::Normal,false));
        painter.setPen(QColor(0,0,0));

        painter.drawText(QRect(X,Y,160,13*aux),Qt::AlignHCenter|Qt::AlignVCenter,
                         multi_line(SBLmodel.record(i).value("PVID").toString(),20));
        painter.drawText(QRect(X+180,Y,500,13*aux),Qt::AlignHCenter|Qt::AlignVCenter,
                         multi_line(SBLmodel.record(i).value("B").toString(),60));
        Y+=13*aux + 8;
    }




}

void printer::SubVotingKeyList(QPainter &painter, QPrinter &printer, QString prop){
    int X=60,Y=50;
    int W=630,H=880;
    int page=1,aux;

    QDir SVKLdir(QDir::currentPath());
    SVKLdir.cd(prop);
    SVKLdir.cd("KG");

    QSqlDatabase SVKL = QSqlDatabase::database("SVKLconex",false);
    SVKL.setDatabaseName(SVKLdir.absoluteFilePath("SubVotingKeyList.db"));
    if(!SVKL.open()){
        return;
    }

// Crea el modelo
    QSqlTableModel SVKLmodel(0,SVKL);
    SVKLmodel.setTable("SubVotingKeyList");
    SVKLmodel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    SVKLmodel.select();

    for (int i = 0; i < SVKLmodel.rowCount(); i++) {

        if(multi_line(SVKLmodel.record(i).value("ev").toString(),25).count("\n") <
           multi_line(SVKLmodel.record(i).value("dz").toString(),68).count("\n"))
            aux=multi_line(SVKLmodel.record(i).value("dz").toString(),68).count("\n")+1;
        else
            aux = multi_line(SVKLmodel.record(i).value("ev").toString(),25).count("\n")+1;

        if((Y+13*aux + 8)>=(H+60)){
            printer.newPage();
            Y=50;
            page++;
        }

        if(Y==50){
        // Imprime el encabezado
            painter.setFont(QFont("Courier", 12, QFont::Bold,true));
            painter.setPen(QColor("dark red"));
            painter.drawText(QRect(X,Y,W,30),Qt::AlignCenter|Qt::AlignBottom,"Sub Voting Key List");
            Y+=50;

            painter.setPen(QColor(0,0,85));
            painter.setFont(QFont("Courier", 8, QFont::Normal,false));
            painter.drawText(QRect(X+90,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"v");
            painter.drawText(QRect(X+W/2+130,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");

            painter.setFont(QFont("Courier", 12, QFont::Normal,false));
            painter.drawText(QRect(X+80,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"e");
            painter.drawText(QRect(X+W/2+120,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"d");

            Y+=30;
            painter.drawText(X+W/2,Y+H+20,QString::number(page));
        }

        if(i%2==0){
            painter.setPen(QColor(230,230,230));
            painter.setBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));}
        else{
            painter.setPen(QColor(255,255,255));
            painter.setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));}

        painter.drawRect(QRectF(X-10,Y-4,W+70,13*aux+8));

        painter.setFont(QFont("Courier", 9, QFont::Normal,false));
        painter.setPen(QColor(0,0,0));
        painter.drawText(QRect(X-10,Y,200,13*aux+4),Qt::AlignCenter|Qt::AlignVCenter,
                         multi_line(SVKLmodel.record(i).value("ev").toString(),25));
        painter.drawText(QRect(X+200,Y,480,13*aux+4),Qt::AlignCenter|Qt::AlignVCenter,
                         multi_line(SVKLmodel.record(i).value("dz").toString(),68));
        Y+=13*aux+8;
    }
}

void printer::SubVoteList(QPainter &painter, QPrinter &printer, QString prop){
    int X=60,Y=50;
    int W=630,H=880;
    int page=1,aux;

    QDir SVLdir(QDir::currentPath());
    SVLdir.cd(prop);
    SVLdir.cd("C");

// Crea la conexion
    QSqlDatabase SVL = QSqlDatabase::database("SVLconex",false);
    SVL.setDatabaseName(SVLdir.absoluteFilePath("SubVoteList.db"));

    if(!SVL.open()){
        return;
    }

// Crea el modelo
    QSqlTableModel SVLmodel(0,SVL);
    SVLmodel.setTable("SubVoteList");
    SVLmodel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    SVLmodel.select();


    for (int i = 0; i < SVLmodel.rowCount(); i+=2) {

        // codificacion hexadecimal para una mejor visualizacion del dato
            string encoded1;
            HexEncoder hex1(new StringSink(encoded1));
            hex1.Put((const byte*)SVLmodel.record(i).value("Ez").toString().data(),SVLmodel.record(i).value("Ez").toString().size());
            hex1.MessageEnd();

        // codificacion hexadecimal para una mejor visualizacion del dato
            string encoded2;
            HexEncoder hex2(new StringSink(encoded2));
            hex2.Put((const byte*)SVLmodel.record(i+1).value("Ez").toString().data(),SVLmodel.record(i).value("Ez").toString().size());
            hex2.MessageEnd();


        if(multi_line(QString::fromStdString(encoded1),40).count("\n") < multi_line(QString::fromStdString(encoded2),40).count("\n"))
            aux=multi_line(QString::fromStdString(encoded2),40).count("\n")+1;
        else
            aux = multi_line(QString::fromStdString(encoded1),40).count("\n")+1;

        if((Y+13*aux + 8)>=(H+60)){
            printer.newPage();
            Y=50;
            page++;
        }

        if(Y==50){
        // Imprime el encabezado
            painter.setFont(QFont("Courier", 12, QFont::Bold,true));
            painter.setPen(QColor("dark red"));
            painter.drawText(QRect(X,Y,W,30),Qt::AlignCenter|Qt::AlignBottom,"Sub Vote List");
            Y+=50;

            painter.setPen(QColor(0,0,85));
            painter.drawText(QRect(X+W/4-20,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"V'");
            painter.drawText(QRect(X+3*W/4-20,Y,20,15),Qt::AlignCenter|Qt::AlignVCenter,"V'");

            painter.setFont(QFont("Courier", 8, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-43,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");
            painter.drawText(QRect(X+W/4+37,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"2");
            painter.drawText(QRect(X+3*W/4-43,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"z");
            painter.drawText(QRect(X+3*W/4+37,Y+5,16,10),Qt::AlignCenter|Qt::AlignVCenter,"2");

            painter.setFont(QFont("Courier", 12, QFont::Normal,false));
            painter.drawText(QRect(X+W/4-50,Y,110,15),Qt::AlignCenter|Qt::AlignVCenter,"E ( ,PVID )");
            painter.drawText(QRect(X+3*W/4-50,Y,110,15),Qt::AlignCenter|Qt::AlignVCenter,"E ( ,PVID )");
            Y+=30;
            painter.drawText(X+W/2,Y+H+20,QString::number(page));
        }

        if(i%4==0){
            painter.setPen(QColor(230,230,230));
            painter.setBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));}
        else{
            painter.setPen(QColor(255,255,255));
            painter.setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));}

        painter.drawRect(QRectF(X+W/4-175,Y-4,320,13*aux+8));
        painter.drawRect(QRect(X+3*W/4-145,Y-4,320,13*aux+8));

        painter.setFont(QFont("Courier", 9, QFont::Normal,false));
        painter.setPen(QColor(0,0,0));

        painter.drawText(QRect(X+W/4-175,Y,320,13*aux+4),Qt::AlignHCenter|Qt::AlignVCenter,
                         multi_line(QString::fromStdString(encoded1),40));
        painter.drawText(QRect(X+3*W/4-145,Y,320,13*aux+4),Qt::AlignHCenter|Qt::AlignVCenter,
                         multi_line(QString::fromStdString(encoded2),40));

        Y+=13*aux+8;
    }
}
